/**
 * @file command_executor_performance.h
 * @brief 命令执行器性能优化定义
 */

#ifndef COMMAND_EXECUTOR_PERFORMANCE_H
#define COMMAND_EXECUTOR_PERFORMANCE_H

#include <Arduino.h>

// ==================== 命令类型枚举 ====================

enum CommandType {
    // 绘图控制命令
    CMD_START = 0,
    CMD_STOP,
    CMD_PAUSE,
    CMD_HOME,
    
    // 扫图命令
    CMD_SWEEP,
    CMD_SWEEPPREP,
    CMD_SWEEPPREV,
    CMD_SWEEPNEXT,
    
    // 图案控制命令
    CMD_NEXT,
    CMD_PREV,
    CMD_LIST,
    CMD_LOAD,
    
    // 系统查询命令
    CMD_STATUS,
    CMD_INFO,
    CMD_TASKINFO,
    CMD_COREINFO,
    CMD_POSITION,
    CMD_HELP,
    
    // 电机控制命令
    CMD_ENABLE,
    CMD_DISABLE,
    CMD_TIMEOUT,
    
    // LED控制命令
    CMD_LEDON,
    CMD_LEDOFF,
    CMD_BRIGHTNESS,
    CMD_DRAWCOLOR,
    
    // 自动播放命令
    CMD_AUTOPLAY_ENABLE,
    CMD_AUTOPLAY_TIME,
    CMD_AUTOPLAY_MODE,
    CMD_AUTOPLAY_STATUS,
    
    // 自动恢复命令
    CMD_AUTORESUME_ENABLE,
    CMD_AUTORESUME_STATUS,
    
    // 未知命令
    CMD_UNKNOWN = 255
};

// ==================== 性能优化常量 ====================

namespace PerformanceConfig {
    static const size_t COMMAND_HASH_TABLE_SIZE = 32;
    static const size_t STRING_BUFFER_SIZE = 64;
    static const size_t PATTERN_BATCH_SIZE = 10;
    static const unsigned long COMMAND_CACHE_TIMEOUT_MS = 5000;
}

// ==================== 命令映射结构 ====================

struct CommandMap {
    const char* command;
    CommandType type;
    uint8_t minLength;  // 最小匹配长度，用于快速过滤
};

// ==================== 性能优化宏 ====================

#define FAST_STRING_COMPARE(str1, str2, len) (strncmp(str1, str2, len) == 0)
#define COMMAND_HASH(str) (simple_hash(str) % PerformanceConfig::COMMAND_HASH_TABLE_SIZE)

// ==================== 内联函数 ====================

// 简单哈希函数
inline uint32_t simple_hash(const char* str) {
    uint32_t hash = 5381;
    while (*str) {
        hash = ((hash << 5) + hash) + *str++;
    }
    return hash;
}

// 快速命令长度检查
inline bool quick_length_check(const String& cmd, uint8_t minLen) {
    return cmd.length() >= minLen;
}

#endif // COMMAND_EXECUTOR_PERFORMANCE_H
